package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private final SpuService spuService;
    @Autowired
    private final SpuDetailService detailService;
    @Autowired
    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    //商品分页查询
//    @GetMapping("/spu/page")
//    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
//            @RequestParam(value = "page", defaultValue = "1") Integer page,
//            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
//            @RequestParam(value = "saleable", required = false) Boolean saleable,
//            @RequestParam(value = "categoryId", required = false) Long categoryId,
//            @RequestParam(value = "brandId", required = false) Long brandId,
//            @RequestParam(value = "id", required = false) Long id) {
//
//        return ResponseEntity.ok(spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
//    }
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>>querySpuByPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id)  {
        return ResponseEntity.ok(this.spuService.querySpuByPage(page,rows,saleable,categoryId,brandId,id));
    }


    //商品新增
    @PostMapping("/spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //商品下架
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //根据id查询商品
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }
    //商品更新
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //根据id查询SpuDetail
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){

        return ResponseEntity.ok(this.detailService.querySpuDetailById(id));
    }
    //根据SpuId查询SKU集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(this.skuService.querySkuBySpuId(id));
    }

    //根据id集合查询SKU集合
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(this.skuService.querySkuBySpuIds(ids));
    }
    //查询商品规格参数键值对
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id, @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(detailService.querySpecValues(id, searching));
    }
    //根据Id查询商品Spu
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(this.spuService.querySpuById(id));
    }

}